package com.brillilab.service.job.user;

import com.brillilab.common.utils.TimeUtil;
import com.brillilab.domain.po.lab.LabMember;
import com.brillilab.domain.po.user.Users;
import com.brillilab.service.core.lab.ILabMemberService;
import com.brillilab.service.core.user.IUsersService;
import com.brillilab.service.logic.message.MessageLogic;
import com.brillilab.service.module.aliyun.AliYunSms;
import com.brillilab.service.module.message.producer.MessageProducer;
import com.brillilab.service.module.message.template.impl.usercenter.BeforeUserExpireTemplate;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zlb
 * @Title: BeforeUserExpireJob
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/10/14 11:07
 */
@Component
public class BeforeUserExpireJob implements Job {
    @Autowired
    private MessageProducer producer;
    @Resource
    private IUsersService usersService;
    @Resource
    private ILabMemberService labMemberService;
    @Autowired
    private MessageLogic messageLogic;
    @Autowired
    private AliYunSms aliYunSms;

    private static Logger logger = LoggerFactory.getLogger(BeforeUserExpireJob.class);

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        long start = System.currentTimeMillis();
        logger.info("账号过期定时任务执行开始..." + start);
        List<Users> usersList = usersService.selectAllTryUserList();
        if (!CollectionUtils.isEmpty(usersList)) {
            for (Users user : usersList) {
                //剩余过期天数
                Long expireDays = TimeUtil.getBetweenDays(System.currentTimeMillis(), user.getExpireDate().getTime());
                if(System.currentTimeMillis()>user.getExpireDate().getTime()){
                    //已过期用户不做提醒
                    continue;
                }
                if (expireDays == 7 || expireDays == 3 || expireDays == 1 || expireDays == 0) {
                    //获取当前用户所在实验室成员信息
                    LabMember labMember = labMemberService.selectMember(user.getLabId(), user.getId());
                    if(labMember!=null) {
                        //TODO 消息推送逻辑
                        logger.info("账号过期定时任务推送-UserID：" + user.getId());
                        producer.sendMessagePush(new BeforeUserExpireTemplate(user.getLabId(), user.getId(), labMember.getId(), expireDays, user.getId()));
                    }

                    if (expireDays == 0) {
                        //账号到期当天短信提醒
                        Map<String,Object> temMap = new HashMap<>();
                        temMap.put("userName",user.getName());
                        messageLogic.sendSmsContent(user.getMobile(),aliYunSms.getWhenUserExpireTemplate(),temMap);
                    }
                }
            }
        }
        logger.info("账号过期定时任务执行结束" + System.currentTimeMillis());
        logger.info("账号过期定时任务执行耗时：" + (System.currentTimeMillis() - start));
    }
}
